package drh.dao.impl;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.handlers.ScalarHandler;

import drh.bean.Users;
import drh.dao.BaseDao;
import drh.dao.UsersDao;
import handler.UsersResultSetHandler;

public class UsersDaoImpl extends BaseDao implements UsersDao {

	@Override
	public Integer deleteUsersById(Integer userId) {
		try {
			return query.update("delete from users where userId=?",userId);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public Integer updateUsers(Users info) {
		try {
			return query.update("update users set logintype=?,username=?,tel=?,pwd=?,email=?,realname=?,time=?,isdelete=? where userId=?",
					info.getLogintype(),info.getUsername(),info.getTel(),info.getPwd(),info.getPwd(),info.getEmail(),info.getRealname(),info.getTime(),info.getIsdelete(),info.getUserId());
		} catch (SQLException e) {	
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public Integer addUsers(Users info) {
		try {
			return query.update("insert into users(logintype,username,tel,pwd,email,realname,time,isdelete) values(?,?,?,?,?,?,?,?)");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public List<Users> GetUsersPaging(Integer pageIndex, Integer pageSize) {
		try {
			return query.query("select * from users limit ?,?",new UsersResultSetHandler(),(pageIndex-1)*pageSize,pageSize);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public Integer getCount() {
		String sql = "select count(1) from orders";
		Object query2;
		try {
			 query2 = query.query(sql, new ScalarHandler());
			return Integer.parseInt(query2.toString());
		} catch (SQLException e) {
	
			e.printStackTrace();
		}
		
		return null;
	}


}
